﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Pr0ject0.Models
{
    public class JobTitleDB
    {
        private string connection;
        private Dictionary<int, string> titles;
        private List<JobTitleModel> models;

        public Dictionary<int, string> jobTitles
        {
            get{ return new Dictionary<int, string>(titles); }
        }

        public List<JobTitleModel> titleList
        {
            get { return new List<JobTitleModel>(models); }
        }

        private volatile static JobTitleDB singleton;
        private static readonly object lockObj = new object();

        public static JobTitleDB GetInstance()
        {
            if (singleton == null)
            {
                lock (lockObj)
                {
                    if (singleton == null) singleton = new JobTitleDB();
                }
            }

            return singleton;
        }

        public static JobTitleDB GetInstance(string connectionString)
        {

            if (singleton == null)
            {
                lock (lockObj)
                {
                    if (singleton == null) singleton = new JobTitleDB(connectionString);
                }
            }

            return singleton;
        }

        private JobTitleDB()
        {
            connection = "DB";
            update();
        }

        private JobTitleDB(string connectionString)
        {
            connection = connectionString;
            update();
        }

        public void AddJob(JobTitleModel model)
        {
            string insert = string.Format("INSERT INTO JobTitle(Title, TitleName, JobDescription) VALUES((Select Max(Title) From JobTitle) + 1, '{0}', '{1}')", model.titleName, model.description);
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connection].ToString());
            SqlCommand cmd = new SqlCommand(insert, conn);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            update();
        }

        public void update()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connection].ToString());
            SqlCommand cmd = new SqlCommand("SELECT * FROM [JobTitle]", conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            titles = new Dictionary<int, string>();
            models = new List<JobTitleModel>();
                
            int title;
            string name;
            JobTitleModel model;

            while (reader.Read())
            {
                model = new JobTitleModel();
                title = Convert.ToInt32(reader["Title"].ToString());
                name = reader["TitleName"].ToString();

                titles[title] = name;
                model.title = title;
                model.titleName = name;
                model.description = reader["JobDescription"].ToString();
                models.Add(model);
            }

            reader.Close();
            conn.Close();
        }
    }
}